<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnClose">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="checkedRole">
      <!-- 选项 -->
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <el-row slot="footer" type="flex" justify="end">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetialByid } from '@/api/user'
import { updateRole } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [], // 获取所有角色
      checkedRole: [], // 选中的角色
      userId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async btnOk() {
      await updateRole({ id: this.userId, roleIds: this.checkedRole })
      this.$message.success('操作成功')
      this.$emit('roleOk')
      this.$emit('update:showRoleDialog', false)
    },
    btnClose() {
      this.checkedRole = []
      this.$emit('update:showRoleDialog', false)
    },
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
    //   console.log(rows)
    },
    async getUserDetialByid(id) {
      this.userId = id
      const { roleIds } = await getUserDetialByid(id)
      this.checkedRole = roleIds
    }
  }
}
</script>

